<script src="../lib/jquerytagsinput/jquery.tagsinput.min.js" ></script>
<link rel="stylesheet" type="text/css" href="css/jquery.tagsinput.css" />
<script>
$(function(){
  $(".taggable").tagsInput({'defaultText':'add an option'});
  $(".pizzaList").accordion({collapsible:true,active:false});
  $(".tagsinput").sortable({

    update: function() {
      var $tags, list, $input;
      // get list of tags in this container
      $tags = $(this).children(".tag").children("span");
      list = [];
      $tags.each(function(){
        list.push($(this).text().trim()); 
      });
      $input = $(this).siblings().first(); // there will only be one...
      $input.importTags(list.join(','));
      }
    });

});

</script>
<?php
require_once(dirname(__FILE__)."/../core/safeParam.php");
echo '
  <style type="text/css">
    input{border:1px solid #CCCCCC; width:200px}
    textarea{border:1px solid #CCCCCC; width:200px}
    .tagsinput{width:500px!important;height:100px!important;}
    .headers:link{color:black!important;}
    .delete{
      position:absolute;
      top:0px;
      right:10px;
      margin-left:10px;
      font-size:.8em;
      }
    .delete:link{color:black!important;}
    .delete img {
      position:relative;
      top:3px
      }
  </style>
';


$pizzas = $database->select("SELECT * FROM pizza where rid  = ".ARID."");

if($_GET['add'] == 'pizza'){ 
 // if I am adding a pizza, just add an empty item to the list of pizzas, and set it's form/action to add instead of edit 
  $addPizza[0]['form'] = '<form action="../functions/menus.php?function=add-pizza" method="post" style="background-color:#FDF9BD;">
    <input type="hidden" name="rid" value="'.ARID.'" />';

  $pizzas = array_merge($addPizza, $pizzas); // place this new 'pizza' at the beginning of the array; it will display as an empty form.
}

if($_GET['edit'] == 'pizza'){ ?> 
  <a href="?edit=pizza&add=pizza" style="color:#000000; text-decoration:none"><img src="add.png" border="0">  Add Pizza</a>  
<?php }
?>
<div class="pizzaList">
<?php

for ($i = 0; $i<count($pizzas); $i += 1){
  // for each pizza....
  $pizza = $pizzas[$i];
  // check for the new/added pizza entry
  $isNew = isset($pizza['form']);
  ?>
    <h3 ><a class="headers" href="#"><?php echo $pizza['name']; ?></a>
    <?php if (!$isNew){ ?>
        <a
          class="delete" 
          href="../functions/menus.php?function=delete-pizza&id=<?php echo $list['id']; ?>"
        >
          <img src="delete.png" border="0"/>
          Delete Pizza
        </a>
    <?php } ?>
    </h3>
    <div>

  <?php


  if (!$isNew) { 
    // if not new, set the form action to edit (otherwise leave it alone)
    $pizza['form'] = '<form action="../functions/menus.php?function=edit-pizza" method="post">';
  }
  
  echo $pizza['form'];
?>

  <input type="hidden" value="<?php echo $pizza['id']; ?>" name="id" />

  <table width="100%" border="0" cellspacing="3" cellpadding="3">
    <tr>
      <td width="150">Name:</td>
      <td><input type="text" name="name" value="<?php echo $pizza['name']; ?>" /></td>
    </tr>
      <tr>
      <td width="150">Size:</td>
      <td><input type="text" name="size" value="<?php echo $pizza['size']; ?>" /></td>
    </tr>
      <tr>
      <td width="150">Base Price:</td>
      <td><input type="text" name="price" value="<?php echo $pizza['price']; ?>" /></td>
    </tr>
          <tr>
      <td width="150">Max Toppings:</td>
      <td><input type="text" name="max_toppings" value="<?php echo $pizza['max_toppings']; ?>"  /></td>
    </tr>
      <tr>
      <td width="150">Per Topping Price:</td>
      <td><input type="text" name="topping_price" value="<?php echo $pizza['topping_price']; ?>" /></td>
    </tr>
       <tr>
      <td width="150">Discount for pickup:</td>
      <td><input type="text" name="discount"  value="<?php echo $pizza['discount']; ?>"  /></td>
    </tr>
         <tr>
      <td width="150">2nd Tier fee:</td>
      <td>Starting at topping # <input type="text" name="tier_amount" value="<?php echo $pizza['tier_amount']; ?>" style="width:100px"  /> Add how much? <input style="width:100px" type="text" name="tier_price" value="<?php echo stripslashes($pizza['tier_price']); ?>"  /> </td>
    </tr>

    <tr>
      <td width="150">Enable sides</td>
      <td>
      
      <?php

          $sides = unserialize($pizza['sides']);

          ?>
      
      None<input type="checkbox" name="sides[0]" value="1" <?php if($sides[0] == 1){ echo 'checked="checked"'; }?>style="width:25px"   /> Left<input style="width:25px"  type="checkbox" name="sides[1]"  value="1" <?php if($sides[1] == 1){ echo 'checked="checked"'; }?>  /> Whole<input style="width:25px"  type="checkbox" name="sides[2]" value="1" <?php if($sides[2] == 1){ echo 'checked="checked"'; }?>  /> Right<input style="width:25px"  type="checkbox" name="sides[3]" value="1" <?php if($sides[3] == 1){ echo 'checked="checked"'; }?>  /> </td>
    </tr>
        <tr>
      <td width="150">Enable first free topping:</td>
      <td><input type="checkbox" name="free_topping" value="1" style="width:25px" <?php if($pizza['free_topping'] == 1){ echo 'checked="checked"'; }?>   /></td>
    </tr>
    <tr>
    <tr>
      <td colspan="2"><hr/>
        <p>Add topping options by typing in the name of the toppings, separated by tab, enter, or ',' (comma). You can drag toppings around to re-order them.</p>
        <p>You can specify an extra charge for any topping by adding the price to the topping name, separated by a ':' (full colon) character, like so: "Pepperoni:1.50".  This amount will be in ADDITION to the the existing 'price per topping' (and 2nd Tier fee) price specified above.</p>
        <p>You can specify that a topping is to be selected/checked by default by adding :checked to the topping like so: "Pepperoni:checked", or "Pepperoni:1.50:checked".  This allows you to build specific pizzas, but still allow customers to customize their toppings.</p>
      </td>
    </tr>
    <tr>
      <td>Pizza base</td>
      <td><input type="text" class="taggable" name="base"  value="<?php echo $pizza['base']; ?>"/></td>
    </tr> 
    <tr>
      <td>Vegetable Toppings</td>
      <td><input type="text" class="taggable" name="toppings"  value="<?php echo $pizza['toppings']; ?>"/></td>
    </tr> 
    <tr>
      <td>Meat Toppings </td>
      <td><input type="text" class="taggable" name="meat_toppings"  value="<?php echo $pizza['meat_toppings']; ?>"/></td>
    </tr>
    <tr>
      <td>Final Toppings</td>
      <td><input type="text" class="taggable" name="topoff"  value="<?php echo $pizza['topoff']; ?>"/></td>
    </tr>
    <tr>
      <td>Sauce</td>
      <td><input type="text" class="taggable" name="sauce"  value="<?php echo $pizza['sauce']; ?>"/></td>
    </tr>
 
     <tr>
      <td>&nbsp;</td>
      <td><input type="submit"  value="Save"/></td>
    </tr>
  </table>
  </form>
</div>
  <?php
}
?>
</div>
